
#include <stdio.h>
#include <memory.h>
#include <malloc.h>
//#include "FormDataParser.h"
#include "FormDataParser_c_api.h"


// int main()
// {
    

//     fstream ifs("./formdata.bin", ios::in | ios::binary);
//     ifs.seekg(0, ifs.end);
//     int size = ifs.tellg();
//     ifs.seekg(ios::beg);
//     cout << "file size : " << size << endl;

//     unique_ptr<char[]> pBuf(new char[size]);
//     ifs.read(pBuf.get(), size);
//     cout << pBuf.get() << endl;

//     std::string boundary("----------------------------334949959704091899130824");

//     std::shared_ptr<std::string> sptr_data(new std::string(pBuf.get()));
//     FormDataParser fdp(sptr_data, 0,boundary);
//     auto p = fdp.parse();

//     printf("********************************\n");
//     for (std::vector<FormItem>::iterator it = p->begin(); it != p->end();++it) {
// 		std::cout << (*(it)).getName() << ":" << (*(it)).getContent() <<std::endl;
// 	}

//     return 0;

// }


int main()
{
    FILE *fp = fopen("./formdata.bin", "rb");
    fseek(fp, 0, SEEK_END);
    int length = ftell(fp);
    rewind(fp);
    char* buf = (char*)malloc(length);
    memset(buf, 0, length);
    fread(buf, length, 1, fp);
    fclose(fp);
    printf("file size : %d\n", length);

    int num = 0;
    FormNoda* formNodas = parser_form_data(buf, length, &num);
    printf("num = %d\n", num);

    
    for(int i = 0; i < num; i++)
    {
        // if(formNodas[i].mName != NULL)
        // {
        //     free(formNodas[i].mName);
        //     formNodas[i].mName = NULL;
        // }

        // if(formNodas[i].mContent != NULL)
        // {
        //     free(formNodas[i].mContent);
        //     formNodas[i].mContent = NULL;
        // }

        //printf("%s:%s\n", formNodas[i].mName, formNodas[i].mContent);

        printf("formNodas[i].mNameLen = %d\n",formNodas[i].mNameLen);
        printf("formNodas[i].mContentLen = %d\n",formNodas[i].mContentLen);
        printf("formNodas[i].mName = %s\n", formNodas[i].mName);
        printf("formNodas[i].mContent = %s\n", formNodas[i].mContent);

    }


    free_form_data(formNodas, num);

    return 0;
}

//gcc main.c -L../target/lib -lFormDataParse -I ../src -o app